﻿package 
{
	import flash.display.Stage;
	import flash.display.MovieClip;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	/* Класс для определения нажатия клавиш */
	public class KeyMaster
	{
		public var isRight:Boolean = false;
		public var isLeft:Boolean = false;
		public var isDown:Boolean = false;
		public var isUp:Boolean = false;
		public var isSpace:Boolean = false;
		public var isBackspace:Boolean = false;
		
		public var isA:Boolean = false;
		public var isS:Boolean = false;
		public var isD:Boolean = false;
		public var isW:Boolean = false;
		
		/* Конструктор */
		public function KeyMaster(thisStage:Stage):void
		{
			//if (thisStage==null)	this.dispose();
			/* Привязываем обработчики к нажатию и отпусканию клавиш */
			thisStage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void { KeyHandler(e, true);});
			thisStage.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void { KeyHandler(e, false);});
		}
		
		/* Обработка нажатия клавиш */
		private function KeyHandler(keyEvent:KeyboardEvent, isKeyDown:Boolean):void
		{
			//trace(keyEvent.keyCode);
			if (keyEvent.keyCode == Keyboard.LEFT)	isLeft=isKeyDown;
			if (keyEvent.keyCode == Keyboard.RIGHT)	isRight=isKeyDown;
			if (keyEvent.keyCode == Keyboard.UP)	isUp=isKeyDown;
			if (keyEvent.keyCode == Keyboard.DOWN)	isDown=isKeyDown;
			if (keyEvent.keyCode == Keyboard.SPACE)	isSpace=isKeyDown;
			if (keyEvent.keyCode == Keyboard.BACKSPACE)	isBackspace=isKeyDown;
			
			if (keyEvent.keyCode == 87)	isW=isKeyDown;
			if (keyEvent.keyCode == 65)	isA=isKeyDown;
			if (keyEvent.keyCode == 83)	isS=isKeyDown;
			if (keyEvent.keyCode == 68)	isD=isKeyDown;
		}
		
	}
}